<template>
	<div class="deposition-warpper">
		<el-tabs v-model="activeName" @tab-click="handleClick">
			<el-tab-pane label="抄录证言" name="deposition">
				<div class="btn-box">
					<el-button type="primary" @click="addDeposition">添加</el-button>
					<el-button type="primary" @click="back">返回</el-button>
				</div>
				<DepositionTable @modifyDeposition="modifyDeposition" @delDeposition="delDeposition" :testimoneyTableData="testimoneyTableData"></DepositionTable>
			</el-tab-pane>
		</el-tabs>
		<DepositionModal
			v-if="showDepositionModal"
			:mode="mode"
			:showDepositionModal="showDepositionModal"
			@submitDeposition="submitDeposition"
			@cancelSubmit="cancelSubmit"
			:depostionContent="depostionContent"
		></DepositionModal>
	</div>
</template>

<script>
import DepositionTable from './DepositionTable.vue';
import DepositionModal from './DepositionModal.vue';
import { getTestimonyList, addTestimony, updateTestimony, deleteTestimony } from '../../../api/api';
export default {
	components: {
		DepositionTable,
		DepositionModal
	},
	data() {
		return {
			activeName: 'deposition',
			showDepositionModal: false,
			depostionContent: '',
			mode: 'add',
			testimoneyTableData: [],
			depostionID: ''
		};
	},
	mounted() {
		this.getTestimonyList();
	},
	methods: {
		async getTestimonyList() {
			try {
				const bizscenarioId = JSON.parse(sessionStorage.getItem('sceneData')).id;
				const { data } = await getTestimonyList(bizscenarioId);
				if (data.code === 200) {
					this.testimoneyTableData = data.message;
				}
				console.log(data, 'getTestimonyList success');
			} catch (error) {
				console.log(error, 'getTestimonyList error');

				this.$message.error('获取证言列表失败');
			}
		},
		async submitDeposition(data) {
			this.depostionContent = data;
			this.showDepositionModal = false;
			const bizscenarioId = JSON.parse(sessionStorage.getItem('sceneData')).id;
			this.operaTionTestiMoney(bizscenarioId, data);
		},

		async operaTionTestiMoney(bizscenarioId, depostionContent) {
			if (this.mode === 'add') {
				try {
					const resp = await addTestimony(bizscenarioId, depostionContent);
					console.log(resp, 'addTestimony');
					this.getTestimonyList();
				} catch (error) {
					console.log(error);
					if (error.code == '6009') {
						this.$message.error(`业务场景启用中，无法进行操作！`);
					} else {
						this.$message.error('添加证言列表失败');
					}
				}
			} else {
				try {
					const resp = await updateTestimony(depostionContent, this.depostionID);
					console.log(resp, 'updateTestimony');
					this.$message({
						message: '修改成功',
						type: 'success'
					});
					this.getTestimonyList();
				} catch (error) {
					console.log(error);
					if (error.code == '6009') {
						this.$message.error(`业务场景启用中，无法进行操作！`);
					} else {
						this.$message.error('修改证言列表失败');
					}
				}
			}
		},

		cancelSubmit() {
			this.showDepositionModal = false;
		},
		addDeposition() {
			this.mode = 'add';
			this.depostionContent = '';
			this.showDepositionModal = true;
		},
		back() {
			this.$router.go(-1);
		},
		modifyDeposition(data) {
			this.mode = 'modify';
			this.depostionContent = data.message;
			this.depostionID = data.id;
			this.showDepositionModal = true;
		},
		async delDeposition(data) {
			console.log(data);
			try {
				const resp = await deleteTestimony(data.id);
				console.log(resp, 'deleteTestimony');
				this.$message({
					message: '删除成功',
					type: 'success'
				});
				this.getTestimonyList();
			} catch (error) {
				console.log(error);
				switch (error.code) {
					case '1002':
						this.$message.warning('请先解除在AI工作流中该抄录证言的配置再进行删除');
						break;
					case '6009':
						this.$message.error(`业务场景启用中，无法进行操作！`);
						break;
					case 6013:
					case '6013':
						this.$message.error(`请先解除在AI工作流中该抄录证言的配置再进行删除`);
						break;
					default:
						this.$message.warning('删除证言失败！');
						break;
				}
			}
		}
	}
};
</script>

<style lang="less" scoped>
.deposition-warpper {
	.btn-box {
		padding-bottom: 10px;
		text-align: right;
	}
}
</style>
